package com.itbaizhan.shopping_seckill_service.redis;

import com.itbaizhan.shopping_common.pojo.CartGoods;
import com.itbaizhan.shopping_common.pojo.Orders;
import com.itbaizhan.shopping_common.pojo.SeckillGoods;
import com.itbaizhan.shopping_common.service.SeckillService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.KeyExpirationEventMessageListener;
import org.springframework.data.redis.listener.RedisMessageListenerContainer;
import org.springframework.stereotype.Component;
/**
 * redis监听类继承KeyExpirationEventMessageListener
 */
@Component
public class RedisKeyExpirationListener extends KeyExpirationEventMessageListener {
	
	@Autowired
	private RedisTemplate redisTemplate;
	@Autowired
	private SeckillService seckillService;
	
	// 构造器
	public RedisKeyExpirationListener(RedisMessageListenerContainer listenerContainer) {
		super(listenerContainer);
	}
	
	/**
	 * 订单过期后，关闭交易，回退商品库存
	 */
	public void onMessage(Message message, byte[] pattern) {
		//获取失效key,订单id
		String expiredKey = message.toString();
		//从redis副本中获取订单信息
		Orders orders = (Orders) redisTemplate.opsForValue().get(expiredKey + "_copy");
		
		Integer num = orders.getCartGoods().get(0).getNum();//商品数量
		Long goodId = orders.getCartGoods().get(0).getGoodId();//商品id
		
		// 查询秒杀商品
		SeckillGoods seckillGoods = seckillService.findSeckillGoodsByRedis(goodId);
		// 回退库存
		seckillGoods.setStockCount(seckillGoods.getStockCount() + num);
		redisTemplate.boundHashOps("seckillGoods").put(goodId, seckillGoods);
		
		// 删除复制订单数据
		redisTemplate.delete(expiredKey+"_copy");
		
	}
}
